import time
from sqlalchemy import create_engine, text

mssql_connection_params = {
    'username': 'sa',
    'password': 'Zhaojianfei0820#',
    'host': '127.0.0.1',
    'port': 1433,
    'database': 'sistarData',
    'driver': 'ODBC+Driver+17+for+SQL+Server'
}

mssql_engine = create_engine(
    f"mssql+pyodbc://{mssql_connection_params['username']}:{mssql_connection_params['password']}@{mssql_connection_params['host']}:{mssql_connection_params['port']}/{mssql_connection_params['database']}?driver={mssql_connection_params['driver']}")


def get_mssql_connection():
    while True:
        try:
            conn = mssql_engine.connect()
            return conn
        except Exception as e:
            print(
                f"MSSQL连接建立失败,请检查网络是否畅通(ping {mssql_connection_params['host']}),服务器是否正常,是否开启远程连接,{str(e)}")
            time.sleep(3)
            continue



mssql_conn = get_mssql_connection()

sql = """SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES where table_name like 'sistar_rt%' or table_name like 'sistar_eng%';"""

data = [row[0] for row in mssql_conn.execute(text(sql)).fetchall()]


print(data)